package com.mspbots.core.wise.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.mspbots.core.AbstractBaseController;
import com.mspbots.core.wise.model.TenantUser;
import com.mspbots.core.wise.service.TenantUserService;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;
import java.util.concurrent.Callable;

@RestController
@RequestMapping("reports")
public class TenantReportsController extends AbstractBaseController<TenantUser, TenantUserService> {


    @GetMapping("user-identifier")
    public Callable selectIdentifier(Long tenantId) {
        return () -> {
            List<TenantUser> tenantUsers = this.service.list(Wrappers.<TenantUser>lambdaQuery().eq(TenantUser::getTenantId, tenantId));
            List<Map<String, String>> userList = new ArrayList<>();
            if (!CollectionUtils.isEmpty(tenantUsers)) {
                tenantUsers.stream().sorted(Comparator.comparing(TenantUser::getFirstname)).forEach(tk ->
                        userList.add(new HashMap<>() {{
                            put("identifier", tk.getIdentifier());
                            put("userName", tk.getFirstname() + " " + tk.getLastname());
                        }}));
            }
            return userList;
        };
    }
}
